package com.yixin.hospital.controller;

import cn.hutool.core.stream.CollectorUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.domain.JobOffice;
import com.yixin.hospital.entity.domain.User;
import com.yixin.hospital.entity.dto.req.joboffice.JobOfficeAddReqDTO;
import com.yixin.hospital.entity.dto.req.joboffice.JobOfficeEditReqDTO;
import com.yixin.hospital.enums.ResponseCodeEnum;
import com.yixin.hospital.service.JobOfficeService;
import com.yixin.hospital.service.UserService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/job-office")
@Api(value = "科室管理", tags = "科室管理")
@Slf4j
public class JobOfficeController {

    @Resource
    private JobOfficeService jobOfficeService;

    @GetMapping("/list")
    @ApiOperation(value = "科室列表")
    public ApiResult list(@RequestParam(value = "userId", required = false) Long userId) {
        return jobOfficeService.allList(userId);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加科室")
    public ApiResult add(@Valid @RequestBody JobOfficeAddReqDTO jobOfficeAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return jobOfficeService.add(jobOfficeAddReqDTO, userName);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑科室")
    public ApiResult edit(@Valid @RequestBody JobOfficeEditReqDTO jobOfficeEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return jobOfficeService.edit(jobOfficeEditReqDTO, userName);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除科室")
    public ApiResult remove(@PathVariable int id) {
        jobOfficeService.removeById(id);
        return ApiResult.success();
    }
}
